import axios, { AxiosInstance } from 'axios';
import  cookies  from './cookies';

// 创建 axios 实例
const instance:AxiosInstance = axios.create({
  baseURL: 'http://localhost:11434',
  timeout: 20000,
  headers: {
    'Content-Type': 'application/json'
  }
});

// 定义不需要token的白名单路径
const whiteList = ['/login', '/register', '/auth/refresh'];
// 请求拦截器
instance.interceptors.request.use(
    config => {
        console.log("configUrl");
        console.log(config?.url);
        
      const isWhiteList = whiteList.some(path => config.url?.includes(path));
      
      if (!isWhiteList) {
        const token = cookies.get('token');  // 从 cookie 获取 token
        if (token) {
          config.headers.Authorization = `Bearer ${token}`;
        }
      }
      return config;
    },
    error => {
      return Promise.reject(error);
    }
  );

// 响应拦截器
instance.interceptors.response.use(
  response => {
    
   return response.data
  },
  error => {
    return Promise.reject(error)
  }
);

// 挂载到全局
window.axios = instance;

export default instance;